﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using Yipusoft.Evaluate.BLL;
using Yipusoft.Evaluate.Model;
using System.Text;
using System.Data;

public partial class Evaluate_BaseInput : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "320302"))
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
        }
        if (!IsPostBack)
        {
            BindYearList();
            BindMonthList();
            CreateTable();
        }
    }
    private void BindYearList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            YearList.Items.Add(li);
        }
        YearList.SelectedIndex = YearList.Items.IndexOf(YearList.Items.FindByValue(DateTime.Now.Month.ToString()));
    }
    private void BindMonthList()
    {
        for (int i = 1; i <= 12; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "月";
            li.Value = i.ToString();
            MonthList.Items.Add(li);
        }
        MonthList.SelectedIndex = MonthList.Items.IndexOf(MonthList.Items.FindByValue(DateTime.Now.Month.ToString()));
    }
    private void CreateTable()
    {
        DataSet ds = SplitPage.ExecuteSQL("SELECT AliasName FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000'");
        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN LEFT(b.DepartmentId,4) = ''' + LEFT(DepartmentId,4) + ''' THEN b.ItemScore END) AS D' + LEFT(DepartmentId,4) + '000' FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000'");
        sql.AppendLine("SET @SQL = 'SELECT a.BaseID, a.BaseName' + @SQL + ' FROM Base a LEFT JOIN BaseItems b ON a.BaseID = b.BaseID AND (b.YearValue=" + YearList.SelectedValue + " AND b.MonthValue="+MonthList.SelectedValue+") OR (b.YearValue IS NULL AND b.MonthValue IS NULL) GROUP BY a.BaseID, a.BaseName'");
        sql.AppendLine("EXEC(@SQL)");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;

        Table tb = new Table();
        tb.Caption = "<span>" + YearList.SelectedItem.Text + "基础成绩评估录入</span>";
        tb.Width = Unit.Percentage(100);
        tb.CellPadding = 4;
        tb.CellSpacing = 0;
        TableRow tr = new TableRow();
        TableCell tc = new TableCell();
        tr.CssClass = "grid_header";
        tc.Text = "";
        tc.Width = Unit.Pixel(30);
        tr.Cells.Add(tc);
        tc = new TableCell();
        tc.HorizontalAlign = HorizontalAlign.Left;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Text = "基础数据项目名称";
        tr.Cells.Add(tc);

        int col = ds.Tables[0].Rows.Count;

        for (int i = 0; i < col; i++)
        {
            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Width = Unit.Pixel(40);
            tc.Style.Add("writing-mode", "tb-lr");
            tc.Style.Add("white-space", "normal");
            tc.Text = ds.Tables[0].Rows[i][0].ToString();
            tr.Cells.Add(tc);
            tb.Rows.Add(tr);
        }
        int row = 1;
        foreach (DataRowView drv in dv)
        {
            tr = new TableRow();
            tr.CssClass = "grid_item";
            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Text = row.ToString();
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.BackColor = System.Drawing.Color.White;
            tc.Text = Base.GetBase((int)drv[0]).BaseName;
            tr.Cells.Add(tc);
            for (int i = 2; i <= col + 1; i++)
            {
                tc = new TableCell();
                tc.BackColor = System.Drawing.Color.White;
                tc.HorizontalAlign = HorizontalAlign.Right;
                string score = "";
                if (drv[i] != DBNull.Value)
                    score = ((decimal)drv[i]).ToString("0");
                else
                    score = "0";
                tc.Text = "<a href=BaseItemInput.aspx?BaseId=" + drv[0].ToString() + "&DepartmentId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + ">" + score + "</a>";
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);
            row++;
        }
        PlaceHolder.Controls.Add(tb);
    }
    protected void YearList_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateTable();
    }
    protected void MonthList_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateTable();
    }
}
